package screen
{
	import aliens.Alien;
	import aliens.AlienController;
	import aliens.BaseAlien;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	import map.MapSource;
	import utils.RectangleHelper;
	import utils.DirectionHelper;
	
	//for debug
	import flash.utils.*;
	
	/**
	 * ...
	 * @author Jeroen Gildemacher
	 */
	public class GameScreen extends Sprite
	{
		//sprites
		private var BASE_ALIEN_DATA:BitmapData = new BitmapData(10, 10, false, 0x000000);
		
		private var skyLayer:SkyLayer;
		private var landscapeLayer:LandscapeLayer;
		private var pixelLayer:PixelLayer;
		
		private var simpleLayer:SimpleLayer;
		
		private var sprites:BitmapData = new BitmapData(500, 500, true, 0x000000);
		private var spriteLayer:Bitmap = new Bitmap(sprites);
		
		private var main:Main;
		private var dx:int = 0;
		private var dy:int = 0;
		private var viewX:int = 0;
		private var viewY:int = 0;
		
		private const SCALEFACTOR:int = 10;
		
		private var noKeyDown:Boolean;
		
		private var alienController:AlienController;
		
		private var mapSource:MapSource=new MapSource;

		
		/** debug stuff
		 * 
		 * */
		 private var followAlien:Boolean = true;
		 private var followedAlien:Alien;
		 private var followedAlienIndex:int=0;
		
		private var taskText:TextField = new TextField();
		private var nextAlienButton:TextField = new TextField;
		private var previousAlienButton:TextField= new TextField;
		 
		public function GameScreen()
		{
			
		}
		
		public function init():void
		{
			skyLayer = new SkyLayer;
			skyLayer.parallaxAmount = 1;
			skyLayer.y = 0;
			skyLayer.startY = 0;// -200;
			skyLayer.layerHeight = 500;// 600;
			addChild(skyLayer);
			
			/*landscapeLayer = new LandscapeLayer;
			landscapeLayer.parallaxAmount = 2;
			landscapeLayer.y = 200;
			landscapeLayer.startY = 200;
			landscapeLayer.layerHeight = 200;
			
			
			addChild(landscapeLayer);
			
			*/
			
			createBitmapDatas();
			

			
			simpleLayer = new SimpleLayer(mapSource.getByteMap(), SCALEFACTOR);
			addChild(simpleLayer);

			spriteLayer.x = 0;
			spriteLayer.y = 0
			addChild(spriteLayer);
			
			
			alienController = new AlienController(mapSource,this);
			
			alienController.createAlien(Alien.BASE_ALIEN, new Point(Math.floor(Math.random()*200+200), 75));
			followedAlien = alienController.aliens[0];
			followedAlienIndex = 0;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
			stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
			
			createTextFields();
			
			
		}
		
		public function drawScreen(e:Event):void
		{
			//skyLayer.y = skyLayer.startY - viewY* skyLayer.parallaxAmount;
			//skyLayer.move(dx);
			
			/*landscapeLayer.y = landscapeLayer.startY - viewY * landscapeLayer.parallaxAmount;
			landscapeLayer.move(dx);
			pixelLayer.move(dx);
			pixelLayer.y=pixelLayer.startY-viewY * pixelLayer.parallaxAmount;
			*/
			//viewX += dx;
			//viewY += dy;
			
			if (followAlien) {
				viewX = followedAlien.mapX-25;
				viewY = followedAlien.mapY- 25;
			} else {
				viewX += dx;
				viewY += dy;
			}
			if (viewX < 0) viewX = 0;
			if (viewX > 950) viewX = 950;
			
			if (viewX < 0) viewX = 0;
			if (viewX > 950) viewX = 950;

			var v:Vector.<Alien> = alienController.getAliensInRectangle(new Rectangle(viewX, viewY, 50, 50));
			var alien:Alien;
			var r:Rectangle;
			
			//for performance reasons lock during update
			sprites.lock();
			sprites.fillRect(sprites.rect, 0);
			
			for (var i:int = 0; i < v.length; i++) {
				alien = v[i];
				sprites.copyPixels(BASE_ALIEN_DATA, new Rectangle(0, 0, 10, 10), new Point((alien.mapX - viewX) * SCALEFACTOR, (alien.mapY - viewY) * SCALEFACTOR));
				if (alien is BaseAlien) {
					if (BaseAlien(alien).carriedColor != -1) {
						r = new Rectangle((alien.mapX - viewX) * SCALEFACTOR, (alien.mapY - viewY + 1) * SCALEFACTOR, SCALEFACTOR, SCALEFACTOR);
						sprites.fillRect(r, mapSource.getColors()[BaseAlien(alien).carriedColor] );
					}
				}
			}
			//don't forget to unlock!
			sprites.unlock();
			
			simpleLayer.drawCanvas(viewX, viewY);
			
			if (noKeyDown) {
				dx = (dx<0)?dx+1:(dx>0)?dx-1:0;
				dy = (dy<0)?dy+1:(dy>0)?dy-1:0;
			}
			alienController.moveAliens();
			var charI:int = getQualifiedClassName(followedAlien.currentTask.subTasks[followedAlien.currentSubTask]).lastIndexOf("::");
			taskText.text = getQualifiedClassName(followedAlien.currentTask.subTasks[followedAlien.currentSubTask]).substring(charI);
		}
		public function key_up(event:KeyboardEvent):void
		{
			noKeyDown = true;
		}
		public function key_down(event:KeyboardEvent):void
		{
			noKeyDown = false;
			if (event.keyCode == Keyboard.LEFT)
			{
				dx--;
				if (dx < -10) dx = -10;
			} 
			if (event.keyCode == Keyboard.RIGHT)
			{
				dx++;
				if (dx > 10) dx = 10;
			}
			if (event.keyCode == Keyboard.UP)
			{
				dy--;
				if (dy < -10) dy = -10;
			} 
			if (event.keyCode == Keyboard.DOWN)
			{
				dy++;
				if (dy > 10) dy = 10;
			} 
		}
		public function followAlienButtonClicked(e:MouseEvent):void {
			followAlien = followAlien?false:true;
			TextField(e.target).text = "follow alien:" + followAlien;
		}
		private function createBitmapDatas():void {
			BASE_ALIEN_DATA = new BitmapData(10, 10, false, 0x000000);
			BASE_ALIEN_DATA .fillRect(new Rectangle(2, 0, 6, 2),255*65536);
			BASE_ALIEN_DATA .fillRect(new Rectangle(0, 2, 10, 2),255*65536);
			BASE_ALIEN_DATA .fillRect(new Rectangle(0, 4, 2, 2),255*65536);
			BASE_ALIEN_DATA .fillRect(new Rectangle(4, 4, 2, 2),255*65536);
			BASE_ALIEN_DATA .fillRect(new Rectangle(8, 4, 2, 2),255*65536);
			BASE_ALIEN_DATA .fillRect(new Rectangle(0, 6, 10, 2),255*65536);
			BASE_ALIEN_DATA .fillRect(new Rectangle(2, 8, 2, 2),255*65536);
			BASE_ALIEN_DATA .fillRect(new Rectangle(6, 8, 2, 2),255*65536);
			
		}
		public function editSimpleLayer(x:int, y:int, color:int):void {
			simpleLayer.scrollingBitmap.setPixel(x, y, color + 0xFF000000);
		}
		
		private function createTextFields():void {
			var followAlienButton:TextField=new TextField;
			followAlienButton.text = "follow alien:" + followAlien;
			followAlienButton.addEventListener(MouseEvent.CLICK, followAlienButtonClicked);
			followAlienButton.x = 550;
			followAlienButton.y = 100;
			followAlienButton.background = true;
			addChild(followAlienButton);
			
			taskText.text="";
			taskText.x = 550;
			taskText.y = 150;
			taskText.background = true;
			addChild(taskText);
			
			previousAlienButton.text="previous Alien";
			previousAlienButton.addEventListener(MouseEvent.CLICK, handlePreviousButtonClicked);
			previousAlienButton.x = 500;
			previousAlienButton.y = 200;
			taskText.background = true;
			addChild(previousAlienButton);
			
			nextAlienButton.text="next Alien";
			nextAlienButton.addEventListener(MouseEvent.CLICK, handleNextButtonClicked);
			nextAlienButton.x = 600;
			nextAlienButton.y = 200;
			nextAlienButton.background = true;
			addChild(nextAlienButton);
			
		}
		public function handleNextButtonClicked(e:Event):void {
			followedAlienIndex = (followedAlienIndex + 1) % alienController.aliens.length;
			followedAlien = alienController.aliens[followedAlienIndex];
		}
		
		public function handlePreviousButtonClicked(e:Event):void {
			followedAlienIndex --;
			if (followedAlienIndex <0) followedAlienIndex= alienController.aliens.length-1;
			followedAlien = alienController.aliens[followedAlienIndex];
		}
	}
	

}
